package org.springshowcase.core.mvc;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springshowcase.core.commons.utils.ControllerUtils;

public class InitBinderSupport {
	// ---------------------------------------------------------------------
	// Class Variable Declaration
	// ---------------------------------------------------------------------
	/** The Constant logger. */
	private static final Logger logger = LoggerFactory
			.getLogger(InitBinderSupport.class);

	// ---------------------------------------------------------------------
	// Constructor Method Declaration
	// ---------------------------------------------------------------------
	// ---------------------------------------------------------------------
	// DI Method (setter,getter) Declaration
	// ---------------------------------------------------------------------
	// ---------------------------------------------------------------------
	// this Method (public/private) Declaration
	// ---------------------------------------------------------------------
	public void initBinder(WebDataBinder binder) throws Exception {
		// TODO Auto-generated method stub
		// super.initBinder(request, binder);
		// 맵핑되는 VO의 멤버 변수 타입이 String일 경우 자동 Trim 처리를 한다.
		binder.registerCustomEditor(String.class,
				new StringTrimmerEditor(false));
		// 맵핑되는 VO의 멤버 변수 타입이 Number형태(Integer,Long...)일 경우 Null값을 허용한다
		// 단 멤버 변수의 값은 Null값이 들어 간다.
		binder.registerCustomEditor(Integer.class, new CustomNumberEditor(
				Integer.class, true));
		binder.registerCustomEditor(Long.class, new CustomNumberEditor(
				Long.class, true));
		binder.registerCustomEditor(Float.class, new CustomNumberEditor(
				Float.class, true));
		binder.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(
				BigDecimal.class, true));
	}
	
	public boolean isMulipart() {
		HttpServletRequest request = ControllerUtils.getCurrentRequest();
		if (ServletFileUpload.isMultipartContent(request)) {
			return true;
		}
		return false;
	}
	
	// ---------------------------------------------------------------------
	// interface implement Method (public/private) Declaration
	// ---------------------------------------------------------------------
	// ---------------------------------------------------------------------
	// abstract implement Method (public/private) Declaration
	// ---------------------------------------------------------------------
	// ---------------------------------------------------------------------
	// abstract Method (public/private) Declaration
	// ---------------------------------------------------------------------
}
